package com.beehoo.dcportal.admin.action;

import java.util.List;

import com.beehoo.dcportal.admin.service.MessageTypeService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.MenuBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class MessageTypeAction extends BaseAction {

	private static final long serialVersionUID = 1931459377419078348L;
	private MessageTypeService messageTypeService;
	private List<MenuBean> resultList;
	private List<MenuBean> parentList;
	private MenuBean menuBean;
	private String menuId;
	private String parentMenu;
	private String newsid;

	public String getNewsid() {
		return newsid;
	}

	public void setNewsid(String newsid) {
		this.newsid = newsid;
	}

	public String getParentMenu() {
		return parentMenu;
	}

	public void setParentMenu(String parentMenu) {
		this.parentMenu = parentMenu;
	}

	public List<MenuBean> getParentList() {
		return parentList;
	}

	public void setParentList(List<MenuBean> parentList) {
		this.parentList = parentList;
	}

	public MenuBean getMenuBean() {
		return menuBean;
	}

	public void setMenuBean(MenuBean menuBean) {
		this.menuBean = menuBean;
	}

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public void setMessageTypeService(MessageTypeService messageTypeService) {
		this.messageTypeService = messageTypeService;
	}

	public List<MenuBean> getResultList() {
		return resultList;
	}

	public void setResultList(List<MenuBean> resultList) {
		this.resultList = resultList;
	}

	public String query() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			menuBean = null;
			parentMenu= CommonUtils.nvlToString(parentMenu);
			if(parentMenu.equals("")){
				resultList = messageTypeService.queryMessageTypeLs(streetId,"1",null);
			}else{
				resultList = messageTypeService.queryMessageTypeLs(streetId,"2",parentMenu);
			}
			super.getRequest().setAttribute("parentMenu", parentMenu);
			parentList = messageTypeService.queryMessageTypeLs(streetId,"1",null);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return INPUT;
	}

	public String initUpdate() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			menuBean = messageTypeService.queryMessageType(streetId, menuId);
			parentList = messageTypeService.queryMessageTypeLs(streetId,"1",null);
			getRequest().setAttribute("method", "update");
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return INPUT;
	}

	public String create() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if(menuBean != null){
			menuBean.setStreetId(streetId);
		}
		try {
			String newTypeId = messageTypeService.createMessageType(menuBean);
			if (CommonUtils.nvlToString(newTypeId).equals("")) {
				throw new BaseException(
						"custom.messages.error.message.type.create.error");
			}
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return SUCCESS;
	}

	public String update() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if(menuBean != null){
			menuBean.setStreetId(streetId);
		}
		try {
			int result = messageTypeService.updateMessageType(menuBean);
			if (result <= 0) {
				getRequest().setAttribute("method", "update");
				throw new BaseException(
						"custom.messages.error.message.type.update.error");
			}
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return SUCCESS;
	}

	public String del() {
		try {
			int result = messageTypeService.delMessageType(menuId);
			if (result <= 0) {
				throw new BaseException(
						"custom.messages.error.message.type.del.error");
			}
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return SUCCESS;
	}
	
	public String initStreetMenu(){
		try {
			messageTypeService.initStreetMenu(newsid);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return "login";
	}
}
